package com.lancoo.klgcourseware.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.base.BaseKlgFragment;
import com.lancoo.klgcourseware.base.BaseKlgPresenter;
import com.lancoo.klgcourseware.entity.ExpressPracticeModel;
import com.lancoo.klgcourseware.entity.HideWordModel;
import com.lancoo.klgcourseware.entity.MoreSlideMenuModel;
import com.lancoo.klgcourseware.entity.WordArticulation;
import com.lancoo.klgcourseware.entity.WordPracticeModel;
import com.lancoo.klgcourseware.entity.bean.ErrorStates;
import com.lancoo.klgcourseware.entity.bean.KlgCommonBean;
import com.lancoo.klgcourseware.entity.bean.KlgDbConstant;
import com.lancoo.klgcourseware.manager.KlgCardDialogManager;
import com.lancoo.klgcourseware.presenter.BasicTrainPresenter;
import com.lancoo.klgcourseware.ui.adapter.ViewPagerFragmentAdapter;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.BasicTrainBaseFragment;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.FillSpellTrainFragment;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.PronunciatioPhraseTrainFragment;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.PronunciatioTrainFragment;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.RepeatTrainFragment;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.SpellMemoryWriteFragment;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.SpellTrainFragment;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.TranslationTrainFragment;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.UsageTrainFragment;
import com.lancoo.klgcourseware.ui.fragment.operate.AlertBaseFragment;
import com.lancoo.klgcourseware.ui.fragment.operate.AlertRPronunceESPTrainOperateFragment;
import com.lancoo.klgcourseware.ui.fragment.operate.AlertRepeatTrainOperateFragment;
import com.lancoo.klgcourseware.ui.fragment.operate.AlertSlideOperateFragment;
import com.lancoo.klgcourseware.ui.fragment.operate.AlertSpellFillTrainOperateFragment;
import com.lancoo.klgcourseware.ui.fragment.operate.AlertSpellMemoryWriteTrainOperateFragment;
import com.lancoo.klgcourseware.ui.fragment.operate.AlertSpellTrainOperateFragment;
import com.lancoo.klgcourseware.ui.fragment.operate.AlertUsageTrainOperateFragment;
import com.lancoo.klgcourseware.ui.fragment.pronounceTrain.PronounceBaseFragment;
import com.lancoo.klgcourseware.ui.widget.CustomColorTransitionPagerTitleView;
import com.lancoo.klgcourseware.ui.widget.NoScrollViewPager;
import com.lancoo.klgcourseware.utils.KlgToolUtils;
import com.lancoo.klgcourseware.view.IBasicTrainView;
import com.stkouyu.entity.LgEvaluateObj;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengmi on 2019/8/15 16:43
 * Description:初级基础训练界面
 */
public class KlgBasicTrainActivity extends KlgBaseTrainActivity implements IBasicTrainView,
        KlgCardDialogManager.KlgCardDismissListener, ViewPager.OnPageChangeListener {

    private static final String TAG = "KlgBasicTrainActivity";
    public static final String KEY_KLGCOMMONBEN="klgCommonBean";

    public static final String KEY_MoreSlideMenuModel="more_slide_menu";

    private KlgCommonBean commonBean;
    private WordArticulation wordArticulation;
    private BasicTrainPresenter mPresenter;
    private NoScrollViewPager viewPager;
    private List<BasicTrainBaseFragment> basicTrainBaseFragmentList;
    private int currentTrainIndex;
    private ViewPager spellAlertviewPager,phoneticAlertviewPager,usageAlertviewPager,fillSpellAlertviewPager,repeatViewpager;
    private String syllable;
    private List<ExpressPracticeModel> expressPracticeModelList;
    private List<String> trainTagList;
    private List<String> moduleAttrs;
    private List<MoreSlideMenuModel> trainRecordList;

    @Override
    protected int getContentViewId() {
        return R.layout.klg_activity_basic_train;
    }

    @Override
    protected String getTitleName() {
        return null;
    }

    @Override
    protected void setViewAndUpdata() {
        commonBean = (KlgCommonBean) getIntent().getSerializableExtra(KEY_KLGCOMMONBEN);
        if(commonBean==null){
            return;
        }
        initTimerMusic();

//        if(!TextUtils.equals("E",commonBean.getKlgType())&&TextUtils.isEmpty(getIntent().getStringExtra(KEY_TRAINTAG))&&getTrainLevel(commonBean.getKlgCode())==TRAIN_LEVEL_JUNIOR){
//            tv_title.setText(commonBean.getKlgName());
//            tv_title.setTextSize(18);
//        }
        mPresenter = new BasicTrainPresenter(this,commonBean.getKlgCode(),this);
        String trainTag=getIntent().getStringExtra(KEY_TRAINTAG);
        if(!TextUtils.isEmpty(trainTag)){
            isLoadSuccess=true;
            enableShowPauseDialog=false;
            invalidateOptionsMenu();
            wordArticulation=(WordArticulation) getIntent().getSerializableExtra(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION);
            expressPracticeModelList = getIntent().getParcelableArrayListExtra(BasicTrainBaseFragment.KEY_USAGE_TRAIN);
            initData(wordArticulation,expressPracticeModelList);
            return;
        }
        if(TextUtils.equals("ESP",commonBean.getKlgType())){
            initData(null,null);
            enableShowPauseDialog=false;
            isLoadSuccess=true;
            invalidateOptionsMenu();
        }else{
            mPresenter.requestInfo();
        }
    }

    public static void launch(Activity mActivity, KlgCommonBean commonBean){
        if(!requestPermission(mActivity)){
            return;
        }
        Intent mIntent=new Intent(mActivity,KlgBasicTrainActivity.class);
        mIntent.putExtra(KEY_KLGCOMMONBEN,commonBean);
        mActivity.startActivityForResult(mIntent,102);
    }
    public static void launch(Activity mActivity, KlgCommonBean commonBean, WordArticulation wordArticulation,
                              List<ExpressPracticeModel> expressPracticeModelList,MoreSlideMenuModel model,String trainTag){
        Intent mIntent=new Intent(mActivity,KlgBasicTrainActivity.class);
        mIntent.putExtra(KEY_KLGCOMMONBEN,commonBean);
        mIntent.putExtra(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);
        mIntent.putParcelableArrayListExtra(BasicTrainBaseFragment.KEY_USAGE_TRAIN,(ArrayList)expressPracticeModelList);
        mIntent.putExtra(KEY_MoreSlideMenuModel,model);
        mIntent.putExtra(KEY_TRAINTAG,trainTag);
        mActivity.startActivityForResult(mIntent,105);
    }

    @Override
    public void loadingSuccess(WordArticulation wordArticulation, BaseKlgPresenter.LoadStatus status) {
        //弃掉，不用
    }

    @Override
    public void showLoadingPg() {
        showLoadingProgress();
    }

    @Override
    public void loadingFailure(ErrorStates errorStates) {
        showFailureLoad(errorStates);
    }

    @Override
    public void loadingSuccess(WordArticulation wordArticulation, List<ExpressPracticeModel> expressPracticeModelList) {
        dismissLoadingLayout();
        this.wordArticulation=wordArticulation;
        //填空拼写
        if(wordArticulation!=null&&TextUtils.equals("C",wordArticulation.getKlgType())&&!TextUtils.isEmpty(wordArticulation.getSyllable())){
            String syllable=wordArticulation.getSyllable().replace("-","·");
            String[] splitFillSpell = syllable.split("·");
            //默认隐藏第0段
            int hideIndex = -1;
            int hideSecondIndex = -1;
            hideIndex =(int)(Math.random()* splitFillSpell.length);
            if(hideIndex == splitFillSpell.length){
                hideIndex = splitFillSpell.length-1;
            }
//            int pointShowIndex=0;
            if(splitFillSpell.length>2){
                while (true){
                    hideSecondIndex =(int)(Math.random()* splitFillSpell.length);
                    if(hideSecondIndex== splitFillSpell.length){
                        hideSecondIndex= splitFillSpell.length-1;
                    }
                    if(hideSecondIndex!= hideIndex){
                        break;
                    }
                }
            }else{

            }
            if(hideSecondIndex<hideIndex){
                int temp=hideIndex;
                hideIndex=hideSecondIndex;
                hideSecondIndex=temp;
            }
            SharedPreferences sharedPreferences = getSharedPreferences(KlgDbConstant.SP_KNOWLEDGE_COURSE, Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putInt(KlgDbConstant.SP_FILL_SPELL_HIDE_FIRSET_INDEX,hideIndex);
            edit.putInt(KlgDbConstant.SP_FILL_SPELL_HIDE_SECOND_INDEX,hideSecondIndex);
            edit.commit();
        }
        initData(wordArticulation,expressPracticeModelList);
        isLoadSuccess=true;
        invalidateOptionsMenu();
    }

    @Override
    protected void reloadData() {
        super.reloadData();
        if(mPresenter!=null){
            mPresenter.requestInfo();
        }
    }

    private void initData(WordArticulation wordArticulation, List<ExpressPracticeModel> expressPracticeModelList){
        initDripSound();
        this.expressPracticeModelList=expressPracticeModelList;
        ViewStub stub = findViewById(R.id.viewstub_main_data);
        trainRecordList=new ArrayList<>();
        trainTagList=new ArrayList<>();
        moduleAttrs=new ArrayList<>();
        if(stub!=null){
            stub.inflate();
            moduleAttrs = new ArrayList<>();
            basicTrainBaseFragmentList = new ArrayList<>();
            addTrainFragment();
            currentTrainTag=trainTagList.get(0);
            MagicIndicator magicIndicator = findViewById(R.id.magic_indicator);
            viewPager = findViewById(R.id.viewpager);
            viewPager.setScroll(true);
            viewPager.addOnPageChangeListener(this);
            currentTrainTag=trainTagList.get(0);
            Log.e("aaaaaaaaaa","initData,currentTrainTag:"+currentTrainTag);

            initViewPagerAndMagicIndicator(magicIndicator, moduleAttrs);

        }
    }

    /**
     * 增加训练模式
     */
    private void addTrainFragment(){
        String trainTag=getIntent().getStringExtra(KEY_TRAINTAG);
        if(!TextUtils.isEmpty(trainTag)){
            addTrainFragment(trainTag);
            return;
        }
        switch (commonBean.getKlgType()){//根据类别区分
            case "C"://单词类型
                addWordTypeTrainFragment();
                break;
            case "D"://词组类型
                addPhraseTypeTrainFragment();
                break;
            case "E"://常用表达式类型
                addExpressionTypeTrainFragment(true);
                break;
            case "F"://句法类型
                addExpressionTypeTrainFragment(false);
                break;
            case "G"://专有名称类型
                addExpressionTypeTrainFragment(false);
                break;
            case "ESP"://ESP类型
                addESPTypeTrainFragment();
                break;
        }
    }

    /**
     * 添加单词类型训练模式
     */
    private void addWordTypeTrainFragment(){
        Bundle mBundle=new Bundle();
        mBundle.putSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);
        mBundle.putSerializable(KEY_KLGCOMMONBEN,commonBean);

        switch (getTrainLevel(commonBean.getKlgCode())){
            case TRAIN_LEVEL_JUNIOR:
                //单词拼读
                moduleAttrs.add(getString(R.string.klg_word_spell));
                SpellTrainFragment spellTrainFragment=new SpellTrainFragment();
                spellTrainFragment.setArguments(mBundle);
                basicTrainBaseFragmentList.add(spellTrainFragment);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_SPELL);
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_word_spell),BasicTrainBaseFragment.TRAINTAG_SPELL,1));

                //发音训练
                moduleAttrs.add(getString(R.string.klg_fayin_train));
                PronunciatioTrainFragment pronunciatioTrainFragment=new PronunciatioTrainFragment();
                basicTrainBaseFragmentList.add(pronunciatioTrainFragment);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_PHONETIC);
                pronunciatioTrainFragment.setArguments(mBundle);
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fayin_train),BasicTrainBaseFragment.TRAINTAG_PHONETIC,1));

                //读句填空
                moduleAttrs.add(getString(R.string.klg_read_sentence_fill));
                UsageTrainFragment usageTrainFragment=new UsageTrainFragment();
                basicTrainBaseFragmentList.add(usageTrainFragment);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_USAGE);
                mBundle.putParcelableArrayList(BasicTrainBaseFragment.KEY_USAGE_TRAIN,(ArrayList) expressPracticeModelList);
                mBundle.putString(BasicTrainBaseFragment.KEY_KLGTYPE,commonBean.getKlgType());
                mBundle.putBoolean(BasicTrainBaseFragment.KEY_ISLASTTRAINMODEL,true);
                usageTrainFragment.setArguments(mBundle);
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_read_sentence_fill),BasicTrainBaseFragment.TRAINTAG_USAGE,1));
                //更多
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fill_spell),BasicTrainBaseFragment.TRAINTAG_SPELL_FILL));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_spell_memory),BasicTrainBaseFragment.TRAINTAG_SPELL_MEMORY_WRITE));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_listen_retall),BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_speek_translation),BasicTrainBaseFragment.TRAINTAG_TRANSLATION));
                break;
            case TRAIN_LEVEL_MIDDLE:
                //全默拼写
                SpellMemoryWriteFragment spellMemoryWriteFragment=new SpellMemoryWriteFragment();
                basicTrainBaseFragmentList.add(spellMemoryWriteFragment);
                spellMemoryWriteFragment.setArguments(mBundle);
                moduleAttrs.add(getString(R.string.klg_spell_memory));
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_SPELL_MEMORY_WRITE);
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_spell_memory),BasicTrainBaseFragment.TRAINTAG_SPELL_MEMORY_WRITE,1));

                //读句填空
                moduleAttrs.add(getString(R.string.klg_read_sentence_fill));
                UsageTrainFragment usageTrainFragment1=new UsageTrainFragment();
                basicTrainBaseFragmentList.add(usageTrainFragment1);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_USAGE);
                mBundle.putParcelableArrayList(BasicTrainBaseFragment.KEY_USAGE_TRAIN,(ArrayList) expressPracticeModelList);
                mBundle.putString(BasicTrainBaseFragment.KEY_KLGTYPE,commonBean.getKlgType());
                mBundle.putBoolean(BasicTrainBaseFragment.KEY_ISLASTTRAINMODEL,false);
                usageTrainFragment1.setArguments(mBundle);
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_read_sentence_fill),BasicTrainBaseFragment.TRAINTAG_USAGE,1));

                //听音复述
                moduleAttrs.add("听音复述");
                RepeatTrainFragment repeatTrainFragment=RepeatTrainFragment.newInstance(expressPracticeModelList);
                basicTrainBaseFragmentList.add(repeatTrainFragment);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT);
                trainRecordList.add(new MoreSlideMenuModel("听音复述",BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT,1));
                //更多
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_speek_translation),BasicTrainBaseFragment.TRAINTAG_TRANSLATION));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fill_spell),BasicTrainBaseFragment.TRAINTAG_SPELL_FILL));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fayin_train),BasicTrainBaseFragment.TRAINTAG_PHONETIC));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_word_spell),BasicTrainBaseFragment.TRAINTAG_SPELL));

                break;
            case TRAIN_LEVEL_HIGH:
                //填空拼写
                FillSpellTrainFragment fillSpellTrainFragment=new FillSpellTrainFragment();
                moduleAttrs.add(getString(R.string.klg_fill_spell));
                basicTrainBaseFragmentList.add(fillSpellTrainFragment);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_SPELL_FILL);
                fillSpellTrainFragment.setArguments(mBundle);
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fill_spell),BasicTrainBaseFragment.TRAINTAG_SPELL_FILL,1));

                //听音复述
                moduleAttrs.add("听音复述");
                RepeatTrainFragment repeatTrainFragment1=RepeatTrainFragment.newInstance(expressPracticeModelList);
                basicTrainBaseFragmentList.add(repeatTrainFragment1);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT);
                trainRecordList.add(new MoreSlideMenuModel("听音复述",BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT,1));
                //更多
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_speek_translation),BasicTrainBaseFragment.TRAINTAG_TRANSLATION));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_read_sentence_fill),BasicTrainBaseFragment.TRAINTAG_USAGE));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fayin_train),BasicTrainBaseFragment.TRAINTAG_PHONETIC));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_spell_memory),BasicTrainBaseFragment.TRAINTAG_SPELL_MEMORY_WRITE));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_word_spell),BasicTrainBaseFragment.TRAINTAG_SPELL));
                break;
        }
        syllable = wordArticulation.getSyllable();
    }

    /**
     * 添加词组类型训练模式
     */
    private void addPhraseTypeTrainFragment(){
        Bundle mBundle=new Bundle();
        mBundle.putSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);
        mBundle.putSerializable(KEY_KLGCOMMONBEN,commonBean);


//        //发音训练
//        PronunciatioPhraseTrainFragment pronunciatioPhraseTrainFragment=new PronunciatioPhraseTrainFragment();
//        moduleAttrs.add(getString(R.string.klg_fayin_train));
//        basicTrainBaseFragmentList.add(pronunciatioPhraseTrainFragment);
//        trainTagList.add(BasicTrainBaseFragment.TRAINTAG_PHONETIC_PHRASE);
//        pronunciatioPhraseTrainFragment.setArguments(mBundle);

        switch (getTrainLevel(commonBean.getKlgCode())){
            case TRAIN_LEVEL_JUNIOR:
                //读句填空
                moduleAttrs.add(getString(R.string.klg_read_sentence_fill));
                UsageTrainFragment usageTrainFragment=new UsageTrainFragment();
                basicTrainBaseFragmentList.add(usageTrainFragment);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_USAGE);
                mBundle.putParcelableArrayList(BasicTrainBaseFragment.KEY_USAGE_TRAIN,(ArrayList) expressPracticeModelList);
                mBundle.putString(BasicTrainBaseFragment.KEY_KLGTYPE,commonBean.getKlgType());
                mBundle.putBoolean(BasicTrainBaseFragment.KEY_ISLASTTRAINMODEL,true);
                usageTrainFragment.setArguments(mBundle);
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_read_sentence_fill),BasicTrainBaseFragment.TRAINTAG_USAGE,1));
                //更多
                if(wordArticulation!=null){
                    trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fayin_train),BasicTrainBaseFragment.TRAINTAG_PHONETIC_PHRASE));
                }
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_listen_retall),BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_speek_translation),BasicTrainBaseFragment.TRAINTAG_TRANSLATION));
                break;
            case TRAIN_LEVEL_MIDDLE:
                //读句填空
                moduleAttrs.add(getString(R.string.klg_read_sentence_fill));
                UsageTrainFragment usageTrainFragment1=new UsageTrainFragment();
                basicTrainBaseFragmentList.add(usageTrainFragment1);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_USAGE);
                mBundle.putParcelableArrayList(BasicTrainBaseFragment.KEY_USAGE_TRAIN,(ArrayList) expressPracticeModelList);
                mBundle.putString(BasicTrainBaseFragment.KEY_KLGTYPE,commonBean.getKlgType());
                mBundle.putBoolean(BasicTrainBaseFragment.KEY_ISLASTTRAINMODEL,false);
                usageTrainFragment1.setArguments(mBundle);
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_read_sentence_fill),BasicTrainBaseFragment.TRAINTAG_USAGE,1));

                //听音复述
                moduleAttrs.add("听音复述");
                RepeatTrainFragment repeatTrainFragment=RepeatTrainFragment.newInstance(expressPracticeModelList);
                basicTrainBaseFragmentList.add(repeatTrainFragment);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT);
                trainRecordList.add(new MoreSlideMenuModel("听音复述",BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT,1));
                //更多
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_speek_translation),BasicTrainBaseFragment.TRAINTAG_TRANSLATION));
                if(wordArticulation!=null){
                    trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fayin_train),BasicTrainBaseFragment.TRAINTAG_PHONETIC_PHRASE));
                }
                break;
            case TRAIN_LEVEL_HIGH:
                //听音复述
                moduleAttrs.add("听音复述");
                RepeatTrainFragment repeatTrainFragment1=RepeatTrainFragment.newInstance(expressPracticeModelList);
                basicTrainBaseFragmentList.add(repeatTrainFragment1);
                trainTagList.add(BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT);
                trainRecordList.add(new MoreSlideMenuModel("听音复述",BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT,1));
                //更多

                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_speek_translation),BasicTrainBaseFragment.TRAINTAG_TRANSLATION));
                trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_read_sentence_fill),BasicTrainBaseFragment.TRAINTAG_USAGE));
                if(wordArticulation!=null){
                    trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fayin_train),BasicTrainBaseFragment.TRAINTAG_PHONETIC_PHRASE));
                }
                break;
        }

    }

    /**
     * 添加常用表达类型,句法训练模式
     */
    private void addExpressionTypeTrainFragment(boolean isExpression){
        Bundle mBundle=new Bundle();
        mBundle.putSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);
        mBundle.putSerializable(KEY_KLGCOMMONBEN,commonBean);
        mBundle.putBoolean(BasicTrainBaseFragment.KEY_ISLASTTRAINMODEL,true);
        moduleAttrs.add(getString(!isExpression?R.string.klg_read_sentence_fill:R.string.klg_situational_conversation));
        UsageTrainFragment usageTrainFragment1=new UsageTrainFragment();
        basicTrainBaseFragmentList.add(usageTrainFragment1);
        trainTagList.add(BasicTrainBaseFragment.TRAINTAG_USAGE);
        mBundle.putParcelableArrayList(BasicTrainBaseFragment.KEY_USAGE_TRAIN,(ArrayList) expressPracticeModelList);
        mBundle.putString(BasicTrainBaseFragment.KEY_KLGTYPE,commonBean.getKlgType());
        mBundle.putBoolean(BasicTrainBaseFragment.KEY_IS_SCENE,isExpression);
        usageTrainFragment1.setArguments(mBundle);
        trainRecordList.add(new MoreSlideMenuModel(getString(!isExpression?R.string.klg_read_sentence_fill:R.string.klg_situational_conversation),BasicTrainBaseFragment.TRAINTAG_USAGE,1));

        //更多
        trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_listen_retall),BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT));
        trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_speek_translation),BasicTrainBaseFragment.TRAINTAG_TRANSLATION));
    }

    /**
     * 添加ESP类型训练模式
     */
    private void addESPTypeTrainFragment(){
        Bundle mBundle=new Bundle();
//        mBundle.putSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);
        mBundle.putSerializable(KEY_KLGCOMMONBEN,commonBean);
        mBundle.putSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);

        //发音训练
        PronunciatioPhraseTrainFragment pronunciatioPhraseTrainFragment=new PronunciatioPhraseTrainFragment();
        moduleAttrs.add(getString(R.string.klg_fayin_train));
        basicTrainBaseFragmentList.add(pronunciatioPhraseTrainFragment);
        trainTagList.add(BasicTrainBaseFragment.TRAINTAG_PHONETIC_PHRASE);
        pronunciatioPhraseTrainFragment.setArguments(mBundle);
        trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_fayin_train),BasicTrainBaseFragment.TRAINTAG_PHONETIC_PHRASE,1));

        //听音复述
        moduleAttrs.add("听音复述");
        if(expressPracticeModelList==null||expressPracticeModelList.size()==0){
            expressPracticeModelList=new ArrayList<>();
            ExpressPracticeModel expressPracticeModel=new ExpressPracticeModel();
            List<WordPracticeModel> OneSentenceList=new ArrayList<>();
            WordPracticeModel wordPracticeModel=new WordPracticeModel();
            wordPracticeModel.setSentence(commonBean.getEnglishMeaning());
            wordPracticeModel.setVoiceTime(commonBean.getMeaningTime());
            wordPracticeModel.setSenvoice(commonBean.getMeaningVoice());
            wordPracticeModel.setHideWordList(new ArrayList<HideWordModel>());
            OneSentenceList.add(wordPracticeModel);
            expressPracticeModel.setOneSentenceList(OneSentenceList);
            expressPracticeModelList.add(expressPracticeModel);
        }
        RepeatTrainFragment repeatTrainFragment=RepeatTrainFragment.newInstance(expressPracticeModelList);
        basicTrainBaseFragmentList.add(repeatTrainFragment);
        trainTagList.add(BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT);
        trainRecordList.add(new MoreSlideMenuModel(getString(R.string.klg_listen_retall),BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT,1));
    }

    private void addTrainFragment(String trainTag){
        MoreSlideMenuModel model=getIntent().getParcelableExtra(KEY_MoreSlideMenuModel);
        trainRecordList.add(model);
        Bundle mBundle=new Bundle();
        mBundle.putSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);
        mBundle.putSerializable(KEY_KLGCOMMONBEN,commonBean);
        mBundle.putParcelableArrayList(BasicTrainBaseFragment.KEY_USAGE_TRAIN,(ArrayList) expressPracticeModelList);
        mBundle.putString(BasicTrainBaseFragment.KEY_KLGTYPE,commonBean.getKlgType());
        mBundle.putBoolean(BasicTrainBaseFragment.KEY_ISMORE_ENTER,true);
        BasicTrainBaseFragment mCurrentFragment=null;
        switch (trainTag){
            case BasicTrainBaseFragment.TRAINTAG_SPELL://单词拼读
                mCurrentFragment=new SpellTrainFragment();
                moduleAttrs.add(getString(R.string.klg_word_spell));
                break;
            case BasicTrainBaseFragment.TRAINTAG_SPELL_FILL://填空拼写
                mCurrentFragment=new FillSpellTrainFragment();
                moduleAttrs.add(getString(R.string.klg_fill_spell));
                break;
            case BasicTrainBaseFragment.TRAINTAG_SPELL_MEMORY_WRITE://全拼默写
                mCurrentFragment=new SpellMemoryWriteFragment();
                moduleAttrs.add(getString(R.string.klg_spell_memory));
                break;
            case BasicTrainBaseFragment.TRAINTAG_PHONETIC://发音训练
                mCurrentFragment=new PronunciatioTrainFragment();
                moduleAttrs.add(getString(R.string.klg_fayin_train));
                break;
            case BasicTrainBaseFragment.TRAINTAG_PHONETIC_PHRASE://词组，esp发音训练
                mCurrentFragment=new PronunciatioPhraseTrainFragment();
                moduleAttrs.add(getString(R.string.klg_fayin_train));
                break;
            case BasicTrainBaseFragment.TRAINTAG_USAGE://用法训练，读句填空
                mCurrentFragment=new UsageTrainFragment();
                moduleAttrs.add(getString(R.string.klg_read_sentence_fill));
                break;
            case BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT://听音复述
                mCurrentFragment=RepeatTrainFragment.newInstance(expressPracticeModelList,true);
                moduleAttrs.add(getString(R.string.klg_listen_retall));
                break;
            case BasicTrainBaseFragment.TRAINTAG_TRANSLATION://口语翻译
//                enableShowPauseDialog=true;
                mCurrentFragment=TranslationTrainFragment.newInstance(expressPracticeModelList);
                moduleAttrs.add(getString(R.string.klg_speek_translation));
                break;
        }
        if(mCurrentFragment!=null){
            if (!trainTag.equals(BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT)
                    &&!trainTag.equals(BasicTrainBaseFragment.TRAINTAG_TRANSLATION)){
                mCurrentFragment.setArguments(mBundle);
            }
            basicTrainBaseFragmentList.add(mCurrentFragment);
            trainTagList.add(trainTag);
        }

    }


    private void initViewPagerAndMagicIndicator(final MagicIndicator magicIndicator,final List<String> cModuleAttrsList){
        ViewPagerFragmentAdapter mAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),basicTrainBaseFragmentList);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(basicTrainBaseFragmentList.size());
        CommonNavigator commonNavigator = new CommonNavigator(this);
        if(basicTrainBaseFragmentList.size()<=3){
            commonNavigator.setAdjustMode(true);
        }
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return cModuleAttrsList==null?0:cModuleAttrsList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new CustomColorTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(Color.parseColor("#81c2ee"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#009de6"));
                simplePagerTitleView.setText(cModuleAttrsList.get(index));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });

                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setColors(Color.TRANSPARENT);
                linePagerIndicator.setLineWidth(KlgToolUtils.dip2px(context,50));
                linePagerIndicator.setRoundRadius(KlgToolUtils.dip2px(context,1.5f));
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setYOffset(UIUtil.dip2px(context, 2));
                return linePagerIndicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }

        });
        magicIndicator.setNavigator(commonNavigator);
//        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
//        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.klg_menu_basic_train,menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.findItem(R.id.menu_train).setVisible(isLoadSuccess);
        menu.findItem(R.id.menu_train).getActionView().findViewById(R.id.img_train_pause).setOnClickListener(this);
        menu.findItem(R.id.menu_train).getActionView().findViewById(R.id.img_card).setOnClickListener(this);
        menu.findItem(R.id.menu_train).getActionView().findViewById(R.id.klg_icon_model_change).setOnClickListener(this);
        menu.findItem(R.id.menu_train).getActionView().findViewById(R.id.img_operate).setOnClickListener(this);
        boolean isShowModelChange=TextUtils.equals("E",commonBean.getKlgType()) //ESP,专有名词，英语常用表达式没有“更多学习”
                ||TextUtils.equals("ESP",commonBean.getKlgType())
                ||TextUtils.equals("G",commonBean.getKlgType());
        menu.findItem(R.id.menu_train).getActionView().findViewById(R.id.klg_icon_model_change).setVisibility(isShowModelChange?View.GONE:View.VISIBLE);
        if(!TextUtils.isEmpty(getIntent().getStringExtra(KEY_TRAINTAG))){
            ( (ImageView) (menu.findItem(R.id.menu_train).getActionView().findViewById(R.id.klg_icon_model_change) ) )
                    .setImageResource(R.mipmap.klg_icon_model_more);
            menu.findItem(R.id.menu_train).getActionView().findViewById(R.id.img_operate).setVisibility(View.GONE);
        }
        if(TextUtils.equals("G",getKlgCommonBean().getKlgType())){
            menu.findItem(R.id.menu_train).getActionView().findViewById(R.id.klg_icon_model_change).setVisibility(View.GONE);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        if(System.currentTimeMillis()-time<500){
            return;
        }
        time=System.currentTimeMillis();
        super.onClick(v);
        int id=v.getId();
        if(id==R.id.img_train_pause){
            Log.e("aaaa","img_train_pause");
            showTrainPauseDialog();
//            showEvaluationErrorDialog();
        }else if(id==R.id.img_card){
            Log.e("aaaa","img_card");
            pauseCurrentTrainProgress();
            KlgCardDialogManager.getInstance().showKnowledgeCardDialog(this,commonBean,true,false,this);
            enableShowPauseDialog=false;
        }else if(id==R.id.img_operate){
            Log.e("aaaa","此功能暂时关闭"+"img_operate");
            pauseCurrentTrainProgress();
            isCurrentTrainModel(currentTrainTag,true);
        }else if (id == R.id.klg_icon_model_change){
            if(!TextUtils.isEmpty(getIntent().getStringExtra(KEY_TRAINTAG))){
                Log.e("aaaa","从全的单个训练模式进入到荐并重新开始");
                saveTrainRecordData(201);
                finish();
            }else{
//                if(temp){
//                    for (int i = 0; i < trainRecordList.size(); i++) {
//                        MoreSlideMenuModel model=trainRecordList.get(i);
//                        model.setIsPass(i<5?1:0);
//                    }
//                    temp=false;
//                }
                KlgMoreActivity.launch(this,REQUEST_TO_MORE,commonBean,wordArticulation,expressPracticeModelList,trainRecordList);
            }

        }
    }
    boolean temp=true;

    @Override
    protected void onPause() {
        super.onPause();
        KlgCardDialogManager.getInstance().pause();
//        onCardDismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        KlgCardDialogManager.getInstance().release();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("aaaaa","requestCode:"+requestCode+"....+resultCode:"+resultCode);
        if (requestCode == REQUEST_TO_MORE||requestCode == 201){
            if (resultCode == KlgMoreActivity.RESULT_CANCEL){// 由“全”回来
                continueTrain();
            }else if (resultCode == KlgMoreActivity.RESULT_EXIT){ //由“全”退出
                finish();
            }else if (resultCode == KlgMoreActivity.RESULT_TRAIN){ //有过训练，从全回来,从第0个模块开始训练
                Log.e("aaaa","有过训练，从全回来,从第0个模块开始训练");
                if(data!=null&&trainRecordList!=null){
                    List<MoreSlideMenuModel> originalTrainRecordList = data.getParcelableArrayListExtra("originalTrainRecordList");
                    for (MoreSlideMenuModel model:originalTrainRecordList){
                        for (MoreSlideMenuModel trainRecord:trainRecordList){
                            if(TextUtils.equals(model.getModuleTag(),trainRecord.getModuleTag())){
                                trainRecord.setIsPass(model.getIsPass());
                                break;
                            }
                        }
                    }
                }
                if(viewPager!=null&&viewPager.getCurrentItem()!=0){
                    viewPager.setCurrentItem(0,false);
                }else{
                    reTrainStart();
                }
            }else if(resultCode==0){
                finish();
            }else if(resultCode==400){
                finish();
            }else if(resultCode==200){
                Log.e("aaaa","重新训练");
                if(data!=null&&trainRecordList!=null){
                    List<MoreSlideMenuModel> originalTrainRecordList = data.getParcelableArrayListExtra("originalTrainRecordList");
                    for (MoreSlideMenuModel model:originalTrainRecordList){
                        for (MoreSlideMenuModel trainRecord:trainRecordList){
                            if(TextUtils.equals(model.getModuleTag(),trainRecord.getModuleTag())){
                                trainRecord.setIsPass(model.getIsPass());
                                break;
                            }
                        }
                    }
                }
                if(viewPager!=null&&viewPager.getCurrentItem()!=0){
                    viewPager.setCurrentItem(0,false);
                    Log.e("aaaa","重新训练22222");
                }else{
                    reTrainStart();
                    Log.e("aaaa","重新训练1111");
                }
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        stopSpeechEvaluation(false);
        pauseStudyTimeTimer();
        pauseCountTimeTimer();
        if(basicTrainBaseFragmentList!=null){
            basicTrainBaseFragmentList.get(currentTrainIndex).pauseTrainStudy(true);
        }
        dismissEvaluationDialog();
        currentTrainIndex=position;
        currentTrainTag=trainTagList.get(position);
        boolean currentTrainModel = isCurrentTrainModel(currentTrainTag,false);
        if(currentTrainModel){
            startTrainCountTime(0);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    public void onCardDismiss(int dismissType,int position,KlgCommonBean klgCommonBean, KlgCardDialogManager.KlgCollectionWordCallBack callBack){
        Log.e("aaaa","onCardDismiss");
        enableShowPauseDialog=true;
        continueTrain();
    }

    /**
     * 继续训练
     */
    protected void continueTrain(){
        Log.e(TAG,"continueTrain");
        if(basicTrainBaseFragmentList!=null){
            basicTrainBaseFragmentList.get(viewPager.getCurrentItem()).resumeTrainStudy();
        }

    }

    /**
     * 重新训练（从当前模块开始）
     */
    protected void reTrainStart(){
        Log.e(TAG,"reTrainStart");
        pauseThreeSecondTimer();
        if(TextUtils.equals(currentTrainTag,BasicTrainBaseFragment.TRAINTAG_SPELL)){//当前模式就是拼写训练
            if(basicTrainBaseFragmentList!=null&&basicTrainBaseFragmentList.size()>0){
                basicTrainBaseFragmentList.get(currentTrainIndex).pauseTrainStudy(true);
                startTrainCountTime(0);
            }
        }else if(TextUtils.equals(currentTrainTag,BasicTrainBaseFragment.TRAINTAG_SPELL_FILL)){//从填空拼写训练进入拼写
            if(basicTrainBaseFragmentList!=null&&basicTrainBaseFragmentList.size()>0){
                basicTrainBaseFragmentList.get(currentTrainIndex).pauseTrainStudy(true);
                startTrainCountTime(0);
            }
        }else if(TextUtils.equals(currentTrainTag,BasicTrainBaseFragment.TRAINTAG_SPELL_MEMORY_WRITE)){//从全拼默写训练进入拼写
            if(basicTrainBaseFragmentList!=null&&basicTrainBaseFragmentList.size()>0){
                basicTrainBaseFragmentList.get(currentTrainIndex).pauseTrainStudy(true);
                startTrainCountTime(0);
            }
        }else if(TextUtils.equals(currentTrainTag,BasicTrainBaseFragment.TRAINTAG_PHONETIC_PHRASE)){//从发音训练进入拼写
            startTrainCountTime(0);
            resetPronunciationTrain();
        }else if(TextUtils.equals(currentTrainTag,BasicTrainBaseFragment.TRAINTAG_PHONETIC)){//从发音训练进入拼写
            resetPronunciationTrain();
//            enterPronounceSubTrainModel(PronounceBaseFragment.PronounceState.TRAINTAG_LISTEN_REPEAT);
        }else if(TextUtils.equals(currentTrainTag,BasicTrainBaseFragment.TRAINTAG_LISTEN_REPEAT)){//从发音训练进入拼写
            if(commonBean==null){
                return;
            }
            startTrainCountTime(0);
            resetUsageTrain();
        }else if(TextUtils.equals(currentTrainTag,BasicTrainBaseFragment.TRAINTAG_TRANSLATION)){//从发音训练进入拼写
            if(commonBean==null){
                return;
            }
            startTrainCountTime(0);
            resetUsageTrain();
        } else{
            if(commonBean==null){
                return;
            }
            resetUsageTrain();
        }
    }


    /**
     * 开启模块训练
     */
    protected void startTrainAfterCountTime(){
        if(basicTrainBaseFragmentList!=null&&currentTrainIndex<basicTrainBaseFragmentList.size()){
            basicTrainBaseFragmentList.get(currentTrainIndex).startSubModeTrain();
        }
    }

    /**
     * 语音评测成功回调
     * @param grade 评测总得分
     * @param url 录音url
     * @param skegnResult 评测详情
     */
    protected void onEvaluationSuccessCallBack(int grade, String url, LgEvaluateObj skegnResult){
        if(basicTrainBaseFragmentList!=null&&currentTrainIndex<basicTrainBaseFragmentList.size()){
            basicTrainBaseFragmentList.get(currentTrainIndex).speechEvaluationSuccess(grade,url,skegnResult);
        }
    }

    /**
     * 进入下个训练模式
     */
    protected void enterNextTrainModel(){
        if(!TextUtils.isEmpty(getIntent().getStringExtra(KEY_TRAINTAG))){//如果trainTag不为空，则判定是从更多训练界面进入，下一步直接返回更多界面
            saveTrainRecordData(101);
            finish();
            return;
        }
        if(currentTrainIndex<basicTrainBaseFragmentList.size()-1){

            currentTrainTag=trainTagList.get(currentTrainIndex+1);
            viewPager.setCurrentItem(currentTrainIndex+1);
        }else{
            enterUsageTrainResultActivity(wordArticulation,trainRecordList,expressPracticeModelList);
        }
    }

    protected KlgCommonBean getKlgCommonBean(){
        return commonBean;
    }


    /**
     * 进入发音训练子模式训练
     */
    protected void enterPronounceSubTrainModel(PronounceBaseFragment.PronounceState pronounceState ){
        Log.e("aaaaaaaaaaaa","currentTrainIndex:"+currentTrainIndex);
        if(basicTrainBaseFragmentList!=null&&currentTrainIndex<basicTrainBaseFragmentList.size()){
            basicTrainBaseFragmentList.get(currentTrainIndex).changePronounceTrainModel(pronounceState);
        }
    }

    /**
     * 暂停当前训练流程训练
     */
    protected void pauseCurrentTrainProgress(){
        stopSpeechEvaluation(false);
        pauseCountTimeTimer();
        pauseStudyTimeTimer();
//        pauseTimerMusic();
//        pauseDripMusicPlay();
        if(basicTrainBaseFragmentList!=null&&currentTrainIndex<basicTrainBaseFragmentList.size()){
            basicTrainBaseFragmentList.get(currentTrainIndex).pauseTrainStudy(false);
        }
    }

    /**
     * 重新开始用法训练
     */
    protected void resetUsageTrain(){
        if(basicTrainBaseFragmentList!=null&&currentTrainIndex<basicTrainBaseFragmentList.size()){
            basicTrainBaseFragmentList.get(currentTrainIndex).restartUsageTrain();
        }
    }


    /**
     * 重新开始发音训练
     */
    protected void resetPronunciationTrain(){
        if(basicTrainBaseFragmentList!=null&&currentTrainIndex<basicTrainBaseFragmentList.size()){
            basicTrainBaseFragmentList.get(currentTrainIndex).restartProunciatioTrain();
        }
    }

    /**
     * 3，2，1倒计时的进度
     * @param progress
     */
    protected void updateCountTimeProgress(int progress,int totalProgress){
        if(basicTrainBaseFragmentList!=null&&currentTrainIndex<basicTrainBaseFragmentList.size()){
            basicTrainBaseFragmentList.get(currentTrainIndex).updateCountTimeProgress(progress,totalProgress);
        }
    }


    /**
     * 更新作答过关结果
     */
    protected void updateCurrentTrainResult(boolean isQuality){
        if(trainRecordList!=null&&currentTrainIndex<trainRecordList.size()){
            trainRecordList.get(currentTrainIndex).setIsPass(isQuality?1:0);
        }
    }

    @Override
    public void onBackPressed() {
        if(!enableShowPauseDialog){
            return;
        }
        saveTrainRecordData(101);
        super.onBackPressed();
    }

    private void saveTrainRecordData(int resultCode){
        Intent mIntent=new Intent();
        if(trainRecordList!=null&&trainRecordList.size()>0){
            MoreSlideMenuModel model = trainRecordList.get(0);
            mIntent.putExtra("trainRecord",model);
        }
        setResult(resultCode,mIntent);
    }

    /*****************引导图*************************/

    public void clostAlertOperate(){
        enableShowPauseDialog=true;
        if(spellAlertviewPager!=null&&spellAlertviewPager.getVisibility()==View.VISIBLE){
            spellAlertviewPager.setVisibility(View.GONE);
        }
        if(phoneticAlertviewPager!=null&&phoneticAlertviewPager.getVisibility()==View.VISIBLE){
            phoneticAlertviewPager.setVisibility(View.GONE);
        }
        if(usageAlertviewPager!=null&&usageAlertviewPager.getVisibility()==View.VISIBLE){
            usageAlertviewPager.setVisibility(View.GONE);
        }
        if(fillSpellAlertviewPager!=null&&fillSpellAlertviewPager.getVisibility()==View.VISIBLE){
            fillSpellAlertviewPager.setVisibility(View.GONE);
        }
        if(repeatViewpager!=null&&repeatViewpager.getVisibility()==View.VISIBLE){
            repeatViewpager.setVisibility(View.GONE);
        }

        if(!haiTrainRecord){
            reTrainStart();
//            startTrainCountTime(0);

        }else{
            continueTrain();
        }

    }

    protected void showSpellTrainOperateAlertDialog(boolean isOnlyShowSpellAlert){
        enableShowPauseDialog=false;
        ViewStub viewStub = findViewById(R.id.viewstub_spell_train_alert);
        if(viewStub!=null){
            viewStub.inflate();
            spellAlertviewPager = findViewById(R.id.alert_viewpager);
            AlertSlideOperateFragment fragment=new AlertSlideOperateFragment();
            AlertSpellTrainOperateFragment alertSpellTrainOperateFragment=new AlertSpellTrainOperateFragment();
            List<BaseKlgFragment> fragmentList=new ArrayList<>();
            if(!isOnlyShowSpellAlert&&currentTrainIndex==0){
                fragmentList.add(fragment);
            }
            fragmentList.add(alertSpellTrainOperateFragment);
            Bundle bundle=new Bundle();
            bundle.putString("klgName",commonBean.getKlgName());
            bundle.putBoolean("isOnlyShowSpellAlert",isOnlyShowSpellAlert||currentTrainIndex!=0);
            bundle.putSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);
            alertSpellTrainOperateFragment.setArguments(bundle);
            ViewPagerFragmentAdapter mAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),fragmentList);
            spellAlertviewPager.setAdapter(mAdapter);
        }else {
            if(spellAlertviewPager!=null){
                spellAlertviewPager.setVisibility(View.VISIBLE);
                spellAlertviewPager.setCurrentItem(0,false);
            }
        }
    }

    /**
     * esp ，词组类型引导图
     */
    protected void showPhoneticTrainOperateAlertDialog(){
        enableShowPauseDialog=false;
        ViewStub viewStub = findViewById(R.id.viewstub_phonetic_train_alert);
        if(viewStub!=null){
            viewStub.inflate();
            phoneticAlertviewPager = findViewById(R.id.alert_phonetic_viewpager);
            AlertBaseFragment alertReadingTrainOperateFragment=null;
            alertReadingTrainOperateFragment=new AlertRPronunceESPTrainOperateFragment();
            List<BaseKlgFragment> fragmentList=new ArrayList<>();
            fragmentList.add(alertReadingTrainOperateFragment);
            Bundle bundle=new Bundle();
            bundle.putString("klgName",commonBean.getKlgName());
            alertReadingTrainOperateFragment.setArguments(bundle);
            ViewPagerFragmentAdapter mAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),fragmentList);
            phoneticAlertviewPager.setAdapter(mAdapter);
        }else {
            if(phoneticAlertviewPager!=null){
                phoneticAlertviewPager.setVisibility(View.VISIBLE);
                phoneticAlertviewPager.setCurrentItem(0,false);
            }
        }
    }

    protected void showUsageTrainOperateAlertDialog(){
        enableShowPauseDialog=false;
        ViewStub viewStub = findViewById(R.id.viewstub_usage_train_alert);
        if(viewStub!=null){
            viewStub.inflate();
            usageAlertviewPager = findViewById(R.id.alert_usage_viewpager);
            AlertBaseFragment alertReadingTrainOperateFragment=new AlertUsageTrainOperateFragment();
            List<BaseKlgFragment> fragmentList=new ArrayList<>();
            fragmentList.add(alertReadingTrainOperateFragment);
            Bundle bundle=new Bundle();
            bundle.putParcelableArrayList(BasicTrainBaseFragment.KEY_USAGE_TRAIN,(ArrayList) expressPracticeModelList);
            alertReadingTrainOperateFragment.setArguments(bundle);
            ViewPagerFragmentAdapter mAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),fragmentList);
            usageAlertviewPager.setAdapter(mAdapter);
        }else{
            if(usageAlertviewPager!=null){
                usageAlertviewPager.setVisibility(View.VISIBLE);
                usageAlertviewPager.setCurrentItem(0,false);
            }
        }
    }


    /**
     * 展示填空拼写训练操作引导图
     */
    protected void showSpellFillTrainOperateAlertDialog(boolean isOnlyShowSpellAlert){
        enableShowPauseDialog=false;
        ViewStub viewStub = findViewById(R.id.viewstub_fill_spell_train_alert);
        if(viewStub!=null){
            viewStub.inflate();
            fillSpellAlertviewPager = findViewById(R.id.alert_fill_spell_viewpager);
            AlertSlideOperateFragment fragment=new AlertSlideOperateFragment();
            AlertSpellFillTrainOperateFragment alertSpellTrainOperateFragment=new AlertSpellFillTrainOperateFragment();
            List<BaseKlgFragment> fragmentList=new ArrayList<>();
            Log.e("abc","第一次加载 展示填空拼写训练操作引导图"+isOnlyShowSpellAlert);
            if(!isOnlyShowSpellAlert&&currentTrainIndex==0){
                fragmentList.add(fragment);
            }
            fragmentList.add(alertSpellTrainOperateFragment);
            Bundle bundle=new Bundle();
            bundle.putBoolean("isOnlyShowSpellAlert",isOnlyShowSpellAlert||currentTrainIndex!=0);
            bundle.putSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);
            alertSpellTrainOperateFragment.setArguments(bundle);
            ViewPagerFragmentAdapter mAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),fragmentList);
            fillSpellAlertviewPager.setAdapter(mAdapter);
        }else {
            if(fillSpellAlertviewPager!=null){
                fillSpellAlertviewPager.setVisibility(View.VISIBLE);
                fillSpellAlertviewPager.setCurrentItem(0,false);
            }
        }
    }


    protected void showSpellMemoryWriteTrainOperateAlertDialog(boolean isOnlyShowSpellAlert){
        enableShowPauseDialog=false;
        ViewStub viewStub = findViewById(R.id.viewstub_spell_train_alert);
        if(viewStub!=null){
            viewStub.inflate();
            spellAlertviewPager = findViewById(R.id.alert_viewpager);
            AlertSlideOperateFragment fragment=new AlertSlideOperateFragment();
            AlertSpellMemoryWriteTrainOperateFragment alertSpellTrainOperateFragment=new AlertSpellMemoryWriteTrainOperateFragment();
            List<BaseKlgFragment> fragmentList=new ArrayList<>();
            if(!isOnlyShowSpellAlert){
                fragmentList.add(fragment);
            }
            fragmentList.add(alertSpellTrainOperateFragment);
            Bundle bundle=new Bundle();
            bundle.putString("klgName",commonBean.getKlgName());
            bundle.putBoolean("isOnlyShowSpellAlert",isOnlyShowSpellAlert);
            bundle.putSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION,wordArticulation);
            alertSpellTrainOperateFragment.setArguments(bundle);
            ViewPagerFragmentAdapter mAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),fragmentList);
            spellAlertviewPager.setAdapter(mAdapter);
        }else {
            if(spellAlertviewPager!=null){
                spellAlertviewPager.setVisibility(View.VISIBLE);
                spellAlertviewPager.setCurrentItem(0,false);
            }
        }
    }

    protected void showRepeatTrainOperateAlertDialog(){
        enableShowPauseDialog=false;
        ViewStub viewStub = findViewById(R.id.viewstub_repeat_train_alert);
        if(viewStub!=null){
            viewStub.inflate();
            repeatViewpager = findViewById(R.id.alert_repeat_viewpager);
            List<BaseKlgFragment> fragmentList=new ArrayList<>();
            fragmentList.add(AlertRepeatTrainOperateFragment.newInstance());
            ViewPagerFragmentAdapter mAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),fragmentList);
            repeatViewpager.setAdapter(mAdapter);
        }else {
            if(repeatViewpager!=null){
                repeatViewpager.setVisibility(View.VISIBLE);
                repeatViewpager.setCurrentItem(0,false);
            }
        }
    }

}
